import 'package:color_dart/color_dart.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertz/components/a_button/index.dart';
import 'package:fluttertz/jsonserialize/tzuser/data.dart';
import 'package:fluttertz/utils/global.dart';

class Zhzx extends StatefulWidget {
  Zhzx({Key key}) : super(key: key);

  _ZhzxState createState() => _ZhzxState();
}

class _ZhzxState extends State<Zhzx> {
  String _mobile = "";

  // 修改電話
  xgdh() async {
    try {

      Response response;
      Dio dio = Dio();
      response = await dio.post(
          "https://aps.acciot.com.tw/api/taizhu/changuserinfo",
          queryParameters: {"adminid": _userData.adminid, "email": _mobile});

      if (response == null) return null;

      Map<String, dynamic> resCallbackMap = response.data;

      int code = resCallbackMap['code'];
      if (code != 1) {
        G.toast('修改電話失敗');
        return null; //返回值不对
      }

      _userData.email = _mobile;

      await G.toast('修改電話成功');
    } catch (e) {
      G.toast('修改電話失敗');
    }
  }

  TzUserData _userData;

  TextEditingController _controller_name;
  TextEditingController _controller_mobile;

  @override
  void initState() {
    super.initState();
    _userData = G.user.data;

    _controller_name = new TextEditingController(text: '${_userData.username}');
    _controller_mobile = new TextEditingController(text: '${_userData.email}');

  }

  @override
  Widget build(BuildContext context) {
    return Container(
        decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage("lib/assets/images/tz/bg.png"),
              fit: BoxFit.cover,
            )),
        child: Scaffold(
          backgroundColor: Colors.transparent,
          resizeToAvoidBottomInset: false,
          appBar: customAppbar(context: context, title: '賬號資訊'),
          body: Container(
            color: Colors.transparent,
            padding: EdgeInsets.only(left: 35, right: 35, top: 87),
            child: Column(
              children: <Widget>[
                /// 用户名
                Container(
                  height: 55,
                  decoration: BoxDecoration(border: G.borderBottom()),
                  child: TextField(
                    keyboardType: TextInputType.text,
                    controller: _controller_name,
                    enabled: false,
                    decoration: InputDecoration(
                        labelText: "",
                        counterText: "",
                        border: InputBorder.none,
                        hintText: '用户名',
                        hintStyle: TextStyle(
                          fontSize: 16,
                        )),
                    onChanged: (e) {
                      setState(() {});
                    },
                  ),
                ),

                /// 電話
                Container(
                  height: 55,
                  decoration: BoxDecoration(border: G.borderBottom()),
                  child: Row(
                    children: <Widget>[
                      Flexible(
                        child: TextField(
                          maxLength: 10,
                          keyboardType: TextInputType.text,
                          controller: _controller_mobile,
                          decoration: InputDecoration(
                              counterText: '',
                              labelText: "",
                              border: InputBorder.none,
                              hintText: '請輸入電話',
                              hintStyle: TextStyle(
                                fontSize: 16,
                              )),
                          onChanged: (e) {
                            setState(() {
                              _mobile = e;
                            });
                          },
                        ),
                      ),
                    ],
                  ),
                ),

                /// 確認
                Container(
                  margin: EdgeInsets.only(top: 30),
                  child: AButton.normal(
                      width: 300,
                      child: Text('確認',
                          style:
                          TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
                      bgColor: rgba(136, 175, 213, 1),
                      color: hex('#fff'),
                      onPressed: () => xgdh()),
                ),

                Container(
                  margin: EdgeInsets.only(top: 20),
                  child: AButton.normal(
                    width: 300,
                    child: Text('修改密碼',
                        style:
                        TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
                    bgColor: rgba(255, 10, 31, 1),
                    color: hex('#fff'),
                    onPressed: () => G.pushNamed('/xgma'),),
                ),
                Container(
                  margin: EdgeInsets.only(top: 20),
                  child: Text('版本號：1.0.44',
                    style:
                    TextStyle(fontSize: 16),
                  ),
                ),
              ],
            ),
          ),
        ));
  }

}
